package servlet;

import service.FavoriteService;
import model.Goods;
import model.Rating;
import model.User;
import service.GoodsService;
import service.RatingService;
import service.ShareService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "goods_detail",urlPatterns = "/goods_detail")
public class GoodsDetailServlet extends HttpServlet {

    private GoodsService gService = new GoodsService();
    private FavoriteService favoriteService = new FavoriteService();
    private RatingService ratingService = new RatingService();
    private ShareService shareService = new ShareService();
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Goods g = gService.getGoodsById(id);
        request.setAttribute("g", g);
        
        // 检查用户是否已收藏该商品
        User user = (User) request.getSession().getAttribute("user");
        boolean isFavorite = false;
        boolean isRated = false;
        Rating userRating = null;
        
        if (user != null) {
            isFavorite = favoriteService.isFavorite(user.getId(), id);
            isRated = ratingService.isRated(user.getId(), id);
            if (isRated) {
                userRating = ratingService.getRatingByUserIdAndGoodsId(user.getId(), id);
            }
        }
        
        // 获取商品评分信息
        List<Rating> ratingList = ratingService.getRatingsByGoodsId(id);
        double averageScore = ratingService.getAverageScoreByGoodsId(id);
        int ratingCount = ratingService.getRatingCountByGoodsId(id);
        
        // 获取商品分享信息
        int shareCount = shareService.getShareCount(id);
        int wechatShareCount = shareService.getShareCountByType(id, "wechat");
        int weiboShareCount = shareService.getShareCountByType(id, "weibo");
        int qqShareCount = shareService.getShareCountByType(id, "qq");
        int linkShareCount = shareService.getShareCountByType(id, "link");
        
        // 设置请求属性
        request.setAttribute("isFavorite", isFavorite);
        request.setAttribute("isRated", isRated);
        request.setAttribute("userRating", userRating);
        request.setAttribute("ratingList", ratingList);
        request.setAttribute("averageScore", averageScore);
        request.setAttribute("ratingCount", ratingCount);
        request.setAttribute("shareCount", shareCount);
        request.setAttribute("wechatShareCount", wechatShareCount);
        request.setAttribute("weiboShareCount", weiboShareCount);
        request.setAttribute("qqShareCount", qqShareCount);
        request.setAttribute("linkShareCount", linkShareCount);
        
        // 如果是从分享链接访问的，记录分享
        String source = request.getParameter("source");
        if ("share".equals(source)) {
            String clientIp = request.getRemoteAddr();
            Integer userId = user != null ? user.getId() : null;
            shareService.recordShare(userId, id, "visit", clientIp);
        }
        
        request.getRequestDispatcher("/goods_detail.jsp").forward(request, response);
    }
}
